package tr.edu.metu.is.mega.server.business;

import java.sql.Timestamp;
import java.util.Date;

import tr.edu.metu.is.mega.common.dto.AnnouncementDTO;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.server.core.AbstractDTOHelper;
import tr.edu.metu.is.mega.server.entity.Announcement;
import tr.edu.metu.is.mega.server.entity.Course;

/**
 * Helper class to convert between DTO and entity.
 */
public class AnnouncementDTOHelper extends AbstractDTOHelper<AnnouncementDTO, Announcement> {

    /**
     * {@inheritDoc}
     */
    @Override
    public AnnouncementDTO convertToDTO(Announcement from) {
        AnnouncementDTO to = new AnnouncementDTO();
        to.setPkid(from.getPkid());
        to.setAnnouncementText(from.getAnnouncementtext());
        to.setCreatedBy(from.getCreatedby());
        to.setDateCreated(new Date(from.getDatecreated().getTime()));

        CourseDTO courseDTO = new CourseDTO();
        courseDTO.setPkid(from.getCourse().getPkid());
        to.setCourse(courseDTO);

        return to;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void convertToEntity(AnnouncementDTO from, Announcement to) {
        to.setPkid(from.getPkid());
        to.setAnnouncementtext(from.getAnnouncementText());
        to.setCreatedby(from.getCreatedBy());
        to.setDatecreated(new Timestamp(from.getDateCreated().getTime()));

        Course course = new Course();
        course.setPkid(from.getCourse().getPkid());
        to.setCourse(course);
    }

}
